#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

struct CompareList
{
    bool operator()(const int &lhs, const int &rhs) const
    {
        cout << "bool operator()" << endl;
        /* return lhs < rhs; */
        return lhs > rhs;
    }
};

void test()
{
    list<int> number = {1, 3, 5, 7, 9, 8, 6, 4, 3, 4, 4, 4, 3};
    display(number);

    cout << endl << "测试unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "测试reverse函数" << endl;
    number.reverse();
    display(number);

    cout << endl << "测试sort函数" << endl;
    number.sort();
    /* number.sort(std::less<int>()); */
    /* number.sort(std::greater<int>()); */
    /* number.sort(CompareList()); */
    display(number);

    cout << endl << "测试unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "测试merge函数" << endl;
    list<int> number2 = {10, 2, 8, 7, 20, 30};
    number2.sort();
    /* number2.sort(std::greater<int>()); */
    display(number2);
    number.merge(number2);
    display(number);
    display(number2);
}

void test2()
{
    list<int> number = {1, 3, 8, 5, 7, 9};
    list<int> number2 = {11, 33, 88, 55, 77, 99};
    display(number);
    display(number2);

#if 0
    cout << endl << endl;
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.splice(it, number2);
    display(number);
    display(number2);
#endif
    cout << endl << endl;
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    auto it2 = number2.end();
    --it2;
    cout << "*it2 = " << *it2 << endl;
    number.splice(it, number2, it2);
    display(number);
    display(number2);

    cout << endl <<endl;
    it2 = number2.begin();
    cout << "*it2 = " << *it2 << endl;
    auto it3 = number2.end();
    --it3;
    --it3;
    cout << "*it3 = " << *it3 << endl;
    number.splice(it, number2, it2, it3);
    display(number);
    display(number2);
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

